﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Trangtrai.Controller.Global;
using System.Data.SqlClient;
using System.Data;

namespace Trangtrai.CauhinhController
{
    public class AoChuanController
    {
        public static void InsertAoChuan(String aochuan_code, String aochuan_name, String ao_code, String season_Name)
        {
            if (ConnectionClass.ConnectToDB())
            {
                SqlCommand insertIntergrate = new SqlCommand("INSERT INTO AOCHUAN (AOCHUAN_CODE, AOCHUAN_NAME, AO_SEASON_ID) VALUES ('"
                        + aochuan_code + "','" + aochuan_name + 
                                "',(SELECT AO_SEASON_ID FROM AO_SEASON WHERE AO_SEASON_ID.SEASON_ID = (SELECT SEASON_ID FROM SEASON WHERE SEASON_NAME='" +
                    season_Name + "') AND AO_SEASON_ID.AO_ID = (SELECT AO_ID FROM AO WHERE AO.AO_CODE = '" + ao_code + "'))", ConnectionClass.sqlConn);
                insertIntergrate.ExecuteNonQuery();
                ConnectionClass.closeConn();
            }
        }

        public static String GetSelectAoName(String ao_code)
        {
            String ao_name = "";
            if (ConnectionClass.ConnectToDB())
            {

                SqlDataAdapter userAdapter = new SqlDataAdapter("Select AO_NAME FROM [AO] WHERE AO_CODE = '" + ao_code
                    + "'", ConnectionClass.sqlConn);


                DataSet user = new DataSet();
                userAdapter.Fill(user, "AO");
                if (user.Tables["AO"].Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    foreach (DataRow pRow in user.Tables["AO"].Rows)
                    {
                        ao_name = pRow["AO_NAME"].ToString();
                    }
                    //SqlDataAdapter roleAdapter = new SqlDataAdapter("Select ROLE_NAME FROM [ROLE] WHERE ROLE_ID = " + roleID, ConnectionClass.sqlConn);
                    ConnectionClass.closeConn();
                    return ao_name;
                }
            }
            else
            {
                ConnectionClass.closeConn();
                return null;
            }
            
        }
    }
}